package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.BallotViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.GiftRankResponse;
import com.jjwxc.jwjskandriod.my.data.UserInfoBg;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BallotActivity extends BaseActivity implements View.OnClickListener {


    TextView tv_competition;
    TextView tv_ballot;
    TextView tv_no;
    private RecyclerView rv_ballot;

    private BallotAdapter adapter;

    private ImageView img_head_one;
    private ImageView img_head_two;
    private ImageView img_head_three;
    private TextView tv_name_one;
    private TextView tv_name_two;
    private TextView tv_name_three;
    private TextView tv_ball_one;
    private TextView tv_ball_two;
    private TextView tv_ball_three;
    private String bookId;

    @Override
    public int getContentViewId() {
        return R.layout.activity_ballot;
    }

    @Override
    public void findView() {
        tv_competition = findViewById(R.id.tv_competition);
        rv_ballot = findViewById(R.id.rv_ballot);
        tv_ballot = findViewById(R.id.tv_ballot);
        tv_no = findViewById(R.id.tv_no);
        img_head_one = findViewById(R.id.img_head_one);
        img_head_two = findViewById(R.id.img_head_two);
        img_head_three = findViewById(R.id.img_head_three);
        tv_name_one = findViewById(R.id.tv_name_one);
        tv_name_two = findViewById(R.id.tv_name_two);
        tv_name_three = findViewById(R.id.tv_name_three);
        tv_ball_one = findViewById(R.id.tv_ball_one);
        tv_ball_two = findViewById(R.id.tv_ball_two);
        tv_ball_three = findViewById(R.id.tv_ball_three);

    }

    @Override
    public void setListener() {
        setNoTitle();
        tv_competition.setOnClickListener(this);
    }

    @Override
    public void afterCreate() {
        rv_ballot.setNestedScrollingEnabled(false);
        rv_ballot.setFocusable(false);
        rv_ballot.setLayoutManager(new LinearLayoutManager(BallotActivity.this));
        rv_ballot.addItemDecoration(new LinearLayouDecoration(BallotActivity.this,
                LinearLayouDecoration.HORIZONTAL, R.dimen.border, R.color.transparent));
        rv_ballot.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new BallotAdapter(this);
        rv_ballot.setAdapter(adapter);

        bookId = getIntent().getStringExtra("bookId");
        listBookGiftRank(bookId);
    }

    public void listBookGiftRank(String bookId) {
        post(Url.listBookGiftRank, null, GiftRankResponse.class, new FFNetWorkCallBack<GiftRankResponse>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(GiftRankResponse response) {
                if (response.getCode() == 200) {
                    List<GiftRankResponse.DataBean.RanksBean> list = response.getData().getRanks();
                    adapter.addData(list);
                    if (response.getData().getDistanceAmount() <= 0) {
                        tv_no.setText("排名第" + response.getData().getUserRank() + "名");
                        tv_ballot.setText("已上榜");
                    } else {
                        tv_no.setText("还差" + response.getData().getDistanceAmount() + "值上榜");
                        tv_ballot.setText("未上榜");
                    }
                    if (list.size() == 3) {
                        if (StUtils.isEmpty(list.get(0).getUserImg())) {
                            img_head_one.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_one, list.get(0).getUserImg());
                            setImgHead(0, list.get(0).getUserImg());
                        }
                        if (StUtils.isEmpty(list.get(1).getUserImg())) {
                            img_head_two.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_two, list.get(1).getUserImg());
                            setImgHead(1, list.get(1).getUserImg());
                        }
                        if (StUtils.isEmpty(list.get(2).getUserImg())) {
                            img_head_three.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_three, list.get(2).getUserImg());
                            setImgHead(2, list.get(2).getUserImg());
                        }
                        tv_name_one.setText(list.get(0).getUsername());
                        tv_name_two.setText(list.get(1).getUsername());
                        tv_name_three.setText(list.get(2).getUsername());
                        tv_ball_one.setText("霸王票:" + list.get(0).getAmount());
                        tv_ball_two.setText("霸王票:" + list.get(1).getAmount());
                        tv_ball_three.setText("霸王票:" + list.get(2).getAmount());
                    } else if (list.size() == 2) {
                        if (StUtils.isEmpty(list.get(0).getUserImg())) {
                            img_head_one.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_one, list.get(0).getUserImg());
                            setImgHead(0, list.get(0).getUserImg());
                        }
                        if (StUtils.isEmpty(list.get(1).getUserImg())) {
                            img_head_two.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_two, list.get(1).getUserImg());
                            setImgHead(1, list.get(1).getUserImg());
                        }
                        tv_name_one.setText(list.get(0).getUsername());
                        tv_name_two.setText(list.get(1).getUsername());
                        tv_ball_one.setText("霸王票:" + list.get(0).getAmount());
                        tv_ball_two.setText("霸王票:" + list.get(1).getAmount());
                    } else if (list.size() == 1) {
                        if (StUtils.isEmpty(list.get(0).getUserImg())) {
                            img_head_one.setImageResource(R.mipmap.icon_head_default);
                        } else {
//                            FFImageLoader.loadCircleImage(BallotActivity.this, img_head_one, list.get(0).getUserImg());
                            setImgHead(0, list.get(0).getUserImg());
                        }
                        tv_name_one.setText(list.get(0).getUsername());
                        tv_ball_one.setText("霸王票:" + list.get(0).getAmount());
                    }
                }

            }

            @Override
            public boolean onFail(GiftRankResponse response) {
                return false;
            }
        }, "{\"offset\":0,\"limit\":10,\"bookId\":\"" + bookId + "\"}");
    }

    /**
     * 设置头像
     * @param type
     * @param userImg
     */
    private void setImgHead(int type, String userImg) {
        int index = Arrays.binarySearch(UserInfoBg.INSTANCE.getUSER_ICON(), userImg);

        if (index >= 0) {
            if (type == 0) {
                img_head_one.setImageResource(UserInfoBg.INSTANCE.getUSER_ICON_RID()[index]);
            }

            if (type == 1) {
                img_head_two.setImageResource(UserInfoBg.INSTANCE.getUSER_ICON_RID()[index]);
            }

            if (type == 2) {
                img_head_three.setImageResource(UserInfoBg.INSTANCE.getUSER_ICON_RID()[index]);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_competition) {
            finish();
        }

    }

    public class BallotAdapter extends BaseRecyclerAdapter<BallotViewHolder> {
        private Context context;


        private List<GiftRankResponse.DataBean.RanksBean> mData = new ArrayList<>();


        public BallotAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<GiftRankResponse.DataBean.RanksBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<GiftRankResponse.DataBean.RanksBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public BallotViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.ballot_item, parent, false);
            BallotViewHolder viewHolder_text = new BallotViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public BallotViewHolder getViewHolder(View view) {
            return new BallotViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint("DefaultLocale")
        @Override
        public void onBindViewHolder(final BallotViewHolder holder, @SuppressLint("RecyclerView") int position, boolean isItem) {
            holder.tv_userName.setText(mData.get(position).getUsername());
            holder.tv_num.setText(String.format("%02d", (position + 1)));
            holder.tv_poll.setText(mData.get(position).getAmount() + "");
//            FFImageLoader.loadCircleImage(context, holder.img_head, mData.get(position).getUserImg());


            if (TextUtils.isEmpty(mData.get(position).getUserImg())) {
                holder.img_head.setImageResource(R.mipmap.icon_head_default);
            } else {
                // 二分查找，判斷是否存在該頭像
                int index = Arrays.binarySearch(UserInfoBg.INSTANCE.getUSER_ICON(), mData.get(position).getUserImg());
                if (index >= 0) {
                    holder.img_head.setImageResource(UserInfoBg.INSTANCE.getUSER_ICON_RID()[index]);
                } else {
                    holder.img_head.setImageResource(R.mipmap.icon_head_default);
                }
            }
        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }


}
